﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace BenevolentSun
{
    class GameOverScreen : Screen
    {
        TextField title, playTimeLbl, playTime, congrats, message;

        Tween fader;

        List<TextField> fields = new List<TextField>();

        Sprite background;

        Sprite logo;

        InstructionsControl instructionsControl;

        DimmerControl dimmerControl;


        public GameOverScreen()
        {
            this.TransitionOffTime = new TimeSpan(0, 0, 0, 0, 100);
        }

        public override void LoadContent()
        {
            AudioManager.StopMusic();

            SpriteFont titleFont = Content.Load<SpriteFont>("Fonts/Screen Title");
            title = new TextField("Game Over", titleFont, new Color(255, 255, 0));
            title.AlignH = AlignH.Center;
            title.Position = new Vector2(1280f / 2f, 10f);
            title.Color.A = 0;

            SpriteFont congratsFont = Content.Load<SpriteFont>("Fonts/Game Over Congrats");
            congrats = new TextField("Congratulations!", congratsFont);
            congrats.AlignH = AlignH.Center;
            congrats.Position = new Vector2(1280f / 2f, 185f);
            congrats.Color.A = 0;

            message = new TextField("You have completed switch!", congratsFont);
            message.AlignH = AlignH.Center;
            message.Position = new Vector2(1280f / 2f, 185f + 100);
            message.Color.A = 0;

            SpriteFont statsFont = Content.Load<SpriteFont>("Fonts/Game Over Stats");
            playTimeLbl = new TextField("Play Time:", statsFont);
            playTimeLbl.Position = new Vector2(220, 425);
            playTimeLbl.Color.A = 0;

            playTime = new TextField("--:--:---", statsFont);
            playTime.AlignH = AlignH.Right;
            playTime.Position = new Vector2(1280 - 220, 425);
            playTime.Color.A = 0;

            fields.Add(title);
            fields.Add(playTime);
            fields.Add(playTimeLbl);
            fields.Add(congrats);
            fields.Add(message);

            logo = new Sprite(Content.Load<Texture2D>("Textures/Gui/Logo"));

            background = new Sprite(Content.Load<Texture2D>("Textures/Gui/Background"));

            instructionsControl = new InstructionsControl(Content);

            dimmerControl = new DimmerControl(Content);
            dimmerControl.FadeIn();
            //dimmerControl.AdvanceToEnd();

            refreshMenuInstructions();

            FadeIn();

            base.LoadContent();
        }

        private void refreshMenuInstructions()
        {
            instructionsControl.HideInstructions();
            instructionsControl.SetInstructions("Press any button to exit.");
            instructionsControl.FadeInInstructions();
        }


        public void FadeIn()
        {
            fader = new Tween(title.Color.A, (byte)(255 * 1f), 0.25, Tween.LoopType.None);
        }

        public void FadeOut()
        {
            fader = new Tween(title.Color.A, 0, 0.25, Tween.LoopType.None);
        }

        public override void Update(GameTime gameTime, bool isFocusAvailable, bool isOccluded)
        {
            if (fader != null)
            {
                fader.Update(gameTime);
                foreach (TextField tf in fields) 
                    tf.Color.A = (byte)fader.Value;

                if (fader.IsFinished)
                    fader = null;
            }

            instructionsControl.Update(gameTime);
            dimmerControl.Update(gameTime);

            base.Update(gameTime, isFocusAvailable, isOccluded);
        }


        public override void HandleInput()
        {
            if (ActionMap.AnyButton)
            {
                CreditsScreen creditsScreen = new CreditsScreen();
                creditsScreen.OnExiting += new EventHandler(creditsScreen_OnExiting);
                ScreenManager.AddScreen(creditsScreen);
            }

            base.HandleInput();
        }


        public override void Draw(GameTime gameTime)
        {
            SpriteBatch.Begin();

            background.Draw(SpriteBatch);

            logo.Draw(SpriteBatch);

            dimmerControl.Draw(SpriteBatch);

            instructionsControl.Draw(SpriteBatch);

            foreach (TextField tf in fields)
                tf.Draw(SpriteBatch);

            SpriteBatch.End();
            base.Draw(gameTime);
        }

        public override void ExitScreen()
        {
            FadeOut();
            instructionsControl.FadeOutInstructions();
            base.ExitScreen();
        }


        void creditsScreen_OnExiting(object sender, EventArgs e)
        {
            ((CreditsScreen)sender).OnExiting -= creditsScreen_OnExiting;
            ScreenManager.AddScreen(new MainMenuScreen());
            Remove();
        }
    
    }

}
